/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.redxiii.util.gui;

import java.util.List;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

/**
 *
 * @author dfilgueiras
 */
public class ProgressUiSample extends javax.swing.JFrame {

    public static class BgFlags {
        private int masterProcess = 0;
        private int middleProcess = 0;
        private int detailProcess = 0;
        private String masterMsg = "";
        private String middleMsg = "";
        private String detailMsg = "";

        private boolean dirty = false;
        
        public String getMasterMsg() {
            return masterMsg;
        }

        public String getMiddleMsg() {
            return middleMsg;
        }

        public String getDetailMsg() {
            return detailMsg;
        }

        public int getMasterProcess() {
            return masterProcess;
        }

        public int getMiddleProcess() {
            return middleProcess;
        }

        public int getDetailProcess() {
            return detailProcess;
        }

        public boolean isDirty() {
            return dirty;
        }
        public void cleanDirtyFlag() {
            this.dirty = false;
        }
        
        public void setMaster(int process, String message) {
            synchronized(this) {
                this.masterProcess = process;
                this.masterMsg = message;
                this.dirty = true;
            }
        }
        
        public void setMiddle(int process, String message) {
            synchronized(this) {
                this.middleProcess = process;
                this.middleMsg = message;
                this.dirty = true;
            }
        }
        
        public void setDetail(int process, String message) {
            synchronized(this) {
                this.detailProcess = process;
                this.detailMsg = message;
                this.dirty = true;
            }
        }
        
        
    }
    
    public static class BgWorker extends SwingWorker<Integer, String> {

        private BgFlags flags;
        private JProgressBar masterProgBar, middleProgBar, detailProgBar;
        private JLabel masterLabel, middleLabel, detailLabel;

        public BgWorker(BgFlags flags, JProgressBar masterProgBar, JProgressBar middleProgBar, JProgressBar detailProgBar, JLabel masterLabel, JLabel middleLabel, JLabel detailLabel) {
            this.flags = flags;
            this.masterProgBar = masterProgBar;
            this.middleProgBar = middleProgBar;
            this.detailProgBar = detailProgBar;
            this.masterLabel = masterLabel;
            this.middleLabel = middleLabel;
            this.detailLabel = detailLabel;
        }
        
        @Override
        protected Integer doInBackground() throws Exception {
            
            while (true) {
                synchronized(flags) {
                    if (flags.isDirty()) {
                        publish("SOME WORK DONE");
                    }
                    if (flags.getMasterProcess() >= 100)
                        break;
                }
                Thread.sleep(500);
            }
            return 1;
        }

        @Override
        protected void process(List<String> chunks) {
            
            masterProgBar.setValue(flags.getMasterProcess());
            masterLabel.setText(flags.getMasterMsg());
            
            middleProgBar.setValue(flags.getMiddleProcess());
            middleLabel.setText(flags.getMiddleMsg());
            
            detailProgBar.setValue(flags.getDetailProcess());
            detailLabel.setText(flags.getDetailMsg());
        }
        
        
        
    }
    /**
     * Creates new form ProgressUiSample
     */
    public ProgressUiSample() {
        initComponents();
        
        
        BgWorker worker = new BgWorker(flags, jProgBarMaster, jProgBarMid, jProgBarDetail, jLabMaster, jLabMid, jLabDetail);
        worker.execute();
    }
    
    BgFlags flags = new BgFlags();

    public BgFlags getFlags() {
        return flags;
    }
    static ProgressUiSample ui;
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollBar1 = new javax.swing.JScrollBar();
        jLabel1 = new javax.swing.JLabel();
        jProgBarMaster = new javax.swing.JProgressBar();
        jLabMaster = new javax.swing.JLabel();
        jProgBarMid = new javax.swing.JProgressBar();
        jLabMid = new javax.swing.JLabel();
        jProgBarDetail = new javax.swing.JProgressBar();
        jLabDetail = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(800, 600));

        jLabel1.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jLabel1.setText("Teste de Progresso");

        jLabMaster.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabMaster.setText("jLabel2");

        jLabMid.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabMid.setText("jLabel2");

        jProgBarDetail.setValue(50);

        jLabDetail.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabDetail.setText("jLabel2");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jProgBarMaster, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabMaster, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jProgBarMid, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabMid, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jProgBarDetail, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabDetail, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jProgBarMaster, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabMaster)
                .addGap(18, 18, 18)
                .addComponent(jProgBarMid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabMid)
                .addGap(18, 18, 18)
                .addComponent(jProgBarDetail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabDetail)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws InterruptedException {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ProgressUiSample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ProgressUiSample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ProgressUiSample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ProgressUiSample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ui = new ProgressUiSample();
                ui.setVisible(true);
            }
        });
        
        while(ui == null) {
            Thread.sleep(500);
        }
        
        BgFlags flags = ui.getFlags();
        for (int c = 1; c <= 5; c++) {
        	flags.setMaster( (c * 100) / 5, "Master Step " + c);
        	
        	for (int d = 1; d <= 10; d++) {
        		flags.setMiddle( (d * 100) / 10, "Middle Step " + d);
        		
                for (int f = 1; f <= 15; f++) {
                    flags.setDetail( (f * 100) / 15, "Detail Step " + f);
                    Thread.sleep(250);
                }
            }
            
        }
       
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabDetail;
    private javax.swing.JLabel jLabMaster;
    private javax.swing.JLabel jLabMid;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JProgressBar jProgBarDetail;
    private javax.swing.JProgressBar jProgBarMaster;
    private javax.swing.JProgressBar jProgBarMid;
    private javax.swing.JScrollBar jScrollBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables
}
